package com.chookss.push;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.alibaba.sdk.android.push.AndroidPopupActivity;
import com.chookss.R;
import com.chookss.base.StaticClass;
import com.chookss.login.SplashActivity;
import com.chookss.tools.UUidUtils;
import com.chookss.tools.Utils;
import com.chookss.tools.XJson;
import com.johnrambo.ktea.ktExtends.ShareUtils;

import java.util.Map;
import java.util.UUID;

/**
 * 辅助推送通道指定打开的弹窗activity,目前包括:小米弹窗、华为弹窗
 */
public class ThirdPushPopupActivity extends AndroidPopupActivity {
    /**
     * 用于其他Activty跳转到该Activity
     *
     * @param context
     */
    private String ANDROID_ID = "";

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ThirdPushPopupActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        Log.i("jia", "actionStart");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_activity_third_push);
        // ActionBar 回退导航
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String summary = intent.getStringExtra("summary");
        ANDROID_ID = Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);
        try {
            ANDROID_ID = UUidUtils.getCustomUUID(ThirdPushPopupActivity.this, ANDROID_ID);
            if(Utils.isNull(ANDROID_ID)){
                String id= new ShareUtils().getString(this, "local_id", "");
                if (Utils.isNull(id)) {
                    String uuid= UUID.randomUUID().toString();
                    ANDROID_ID=uuid;
                    new ShareUtils().putString(this, "local_id", uuid);
                }else{
                    ANDROID_ID=id;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /**
     * 弹窗消息打开互调。辅助弹窗通知被点击时,此回调会被调用,用户可以从该回调中获取相关参数进行下一步处理
     *
     * @param title
     * @param content
     * @param extraMap
     */
    @Override
    protected void onSysNoticeOpened(String title, String content, Map<String, String> extraMap) {
        new ShareUtils().putString(ThirdPushPopupActivity.this, StaticClass.PUSH_DATA, XJson.mapToJsonObj(extraMap));
        Intent intentHome = new Intent(this, SplashActivity.class);
        startActivity(intentHome);
        finish();
    }
}

